//
//  ExitCityViewController.m
//  jingjing2.0
//
//  Created by apple on 16/5/25.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ExitCityViewController.h"

@interface ExitCityViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@end

@implementation ExitCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.title=@"选择城市";
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    
}
#pragma mark--进来和出去的一些设置
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [GeneralTool hideTabBar];
    //刷新数据
    [self getExitedCity];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [GeneralTool showTabBar];
    
}

#pragma mark--获取开通的省份

-(void)getExitedCity
{
    //http://192.168.1.107:3722/Activity/CityAll/{ProvinceCode}/{Timestamp}/{Sign}
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSString *timeStamp=[GeneralTool gettimestamp];
    
    NSDictionary *signDic=@{@"ProvinceCode":_provinceCode,
                            @"Timestamp":timeStamp};
    
    NSString *sign=[GeneralTool GetSin:signDic];
    
    NSString *urlString=[NSString stringWithFormat:@"Activity/CityAll/%@/%@/%@",_provinceCode,timeStamp,sign];
    
    [RequestTool requestDataWithType:@"get" andUrlString:urlString andParameters:nil andRequestFinished:^(id responses, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        if (error) {
            [MBProgressHUD showError:@"获取称市列表失败，请重新尝试"];
        }
        if ([responses[@"IsSuccess"]intValue]==1) {
            self.cityArr=responses[@"ReturnObject"];
            [_tableView reloadData];
        }else
        {
            [MBProgressHUD showError:responses[@"ReturnString"]];
        }
        
    }];
    
}

#pragma mark--表
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, kFrame.size.width, kFrame.size.height-64) style:UITableViewStyleGrouped];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.cityArr.count;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"请选择城市";
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    NSDictionary *dic=self.cityArr[indexPath.row];
    cell.textLabel.text=dic[@"CityName"];
    cell.tag=[dic[@"CityCode"]integerValue];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[_tableView cellForRowAtIndexPath:indexPath];
    NSString *cityCode=[NSString stringWithFormat:@"%zd",cell.tag];
    
    _passCityBlock(cell.textLabel.text,cityCode);
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
@end
